package imageprocessing;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Util 
{
	public static BufferedImage loadImage(String fileName)
	{
		try 
		{
			BufferedImage image = ImageIO.read(new File(fileName));
			return image;
		} 
		catch (IOException e) 
		{
			return null;
		}
	}
	
	public static boolean saveImage(BufferedImage image, String fileName)
	{
		String lowerName = fileName.toLowerCase();
		String format = "JPG";
		
		if(lowerName.endsWith(".jpg"))
			format = "JPG";
		else if(lowerName.endsWith(".png"))
			format = "PNG";
		else if(lowerName.endsWith(".bmp"))
			format = "BMP";
		else
		{
			System.out.println("Invalid filename/format");
			return false;
		}
		
		try 
		{
			ImageIO.write(image, format, new File(fileName));
		} 
		catch (IOException e) 
		{
			return false;
		}
		
		return true;
	}
	
	public static BufferedImage sameCanvasSize(BufferedImage ref)
	{
		return new BufferedImage(ref.getWidth(), ref.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
	}
}
